package com.simplemobiletools.camera.activities

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.extensions.Permission
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.PERMISSION_CAMERA
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE

class LaunchActivity : AppCompatActivity() {

    val permissio = Permission()

    @SuppressLint("MissingSuperCall")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_launch)
        findViewById<View>(R.id.to_main).post {
            permissio.handlePermission(this, PERMISSION_CAMERA) { camera ->
                if (camera) {
                    permissio.handlePermission(this, PERMISSION_WRITE_STORAGE) { write ->
                        if (write) {
                            startActivity(Intent(this, MainActivity::class.java))
                        } else {
                            toast(R.string.no_storage_permissions)
                            finish()
                        }
                    }
                } else {
                    toast(R.string.no_camera_permissions)
                    finish()
                }
            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        permissio.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

}
